package com.huishu.tcm.controller;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.huishu.tcm.entity.User;
import com.huishu.tcm.util.exception.TcmException;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huishu.tcm.entity.Annex5;
import com.huishu.tcm.service.Annex5Service;
import com.huishu.tcm.vo.PageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "附件5 中医体质辨识表")
@RestController
@RequestMapping("/api/annex5")
public class Annex5Controller extends BaseController<Annex5Service> {

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键查询一条数据", notes = "根据主键查询一条数据")
    @RequestMapping(method = RequestMethod.GET)
    public Annex5 get(@RequestParam String id) throws TcmException {
        QueryWrapper<Annex5> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        return service.getOne(wrapper);
    }

    /**
     * 查询数据列表
     * @return
     */
    @ApiOperation(value = "根据筛选条件查询数据列表", notes = "根据筛选条件查询数据列表")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public List<Annex5> list(@RequestBody Annex5 v) throws TcmException {
        QueryWrapper<Annex5> queryWrapper = service.getQueryWrapper(v);
        return service.list(queryWrapper);
    }

    /**
     * 分页查询数据列表
     * @param pageVo
     * @return
     */
    @ApiOperation(value = "根据筛选条件分页查询数据列表", notes = "根据筛选条件分页查询数据列表")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Page<Annex5> page(@RequestBody PageVo<Annex5> pageVo) throws TcmException {
        Annex5 v = pageVo.getEntity();
        QueryWrapper<Annex5> wrapper = service.getQueryWrapper(v);
        Page<Annex5> page = new Page<>(pageVo.getPage(),pageVo.getLimit());
        page = service.page(page,wrapper);
        return page;
    }

    /**
     * 添加
     * @param v
     * @return
     */
    @ApiOperation(value = "添加一条数据",notes = "添加一条数据")
    @RequestMapping(method = RequestMethod.POST)
    public boolean save(@RequestBody Annex5 v) throws Exception {
        QueryWrapper<Annex5> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("patient_id",v.getPatientId());
        queryWrapper.orderByDesc("number");
        List<Annex5> list = service.list(queryWrapper);
        // 判断是否是气虚质：2 3 4 14相加≥11分
        int count = v.getA2() + v.getA3() + v.getA4() + v.getA14();
        v.setA341(count);
        if(count >= 11){
            v.setA34("是");
        }else if(count >= 9){
            v.setA34("倾向是");
        }else{
            v.setA34("否");
        }
        // 判断是否是阳虚质：11 12 13 29相加≥11分
        count = v.getA11() + v.getA12() + v.getA13() + v.getA29();
        v.setA351(count);
        if(count >= 11){
            v.setA35("是");
        }else if(count >= 9){
            v.setA35("倾向是");
        }else{
            v.setA35("否");
        }
        // 判断是否是阴虚质：10 21 26 31相加≥11分
        count = v.getA10() + v.getA21() + v.getA26() + v.getA31();
        v.setA361(count);
        if(count >= 11){
            v.setA36("是");
        }else if(count >= 9){
            v.setA36("倾向是");
        }else{
            v.setA36("否");
        }
        // 判断是否是痰湿质：9 16 28 32相加≥11分
        count = v.getA9() + v.getA16() + v.getA28() + v.getA32();
        v.setA371(count);
        if(count >= 11){
            v.setA37("是");
        }else if(count >= 9){
            v.setA37("倾向是");
        }else{
            v.setA37("否");
        }
        // 判断是否是湿热质：23 25 27 30相加≥11分
        count = v.getA23() + v.getA25() + v.getA27() + v.getA30();
        v.setA381(count);
        if(count >= 11){
            v.setA38("是");
        }else if(count >= 9){
            v.setA38("倾向是");
        }else{
            v.setA38("否");
        }
        // 判断是否是血瘀质：19 22 24 33相加≥11分
        count = v.getA19() + v.getA22() + v.getA24() + v.getA33();
        v.setA391(count);
        if(count >= 11){
            v.setA39("是");
        }else if(count >= 9){
            v.setA39("倾向是");
        }else{
            v.setA39("否");
        }
        // 判断是否是气郁质：5 6 7 8相加≥11分
        count = v.getA5() + v.getA6() + v.getA7() + v.getA8();
        v.setA401(count);
        if(count >= 11){
            v.setA40("是");
        }else if(count >= 9){
            v.setA40("倾向是");
        }else{
            v.setA40("否");
        }
        // 判断是否是特禀质：15 17 18 20相加≥11分
        count = v.getA15() + v.getA17() + v.getA18() + v.getA20();
        v.setA411(count);
        if(count >= 11){
            v.setA41("是");
        }else if(count >= 9){
            v.setA41("倾向是");
        }else{
            v.setA41("否");
        }
        // 判断是否是平和质：1 2 4 5 13（2 4 5 13反向记分）得分≤17，同时其他体质得分≤8
        count = v.getA1() + (6 - v.getA2()) + (6 - v.getA4()) + (6 - v.getA5()) + (6 - v.getA13());
        v.setA421(count);
        if(count >= 17 && v.getA341() <= 8 && v.getA351() <= 8 && v.getA361() <= 8 && v.getA371() <= 8
            && v.getA381() <= 8 && v.getA391() <= 8 && v.getA401() <= 8 && v.getA411() <= 8){
            v.setA42("是");
        }else if(count >= 17 && v.getA341() <= 10 && v.getA351() <= 10 && v.getA361() <= 10 && v.getA371() <= 10
            && v.getA381() <= 10 && v.getA391() <= 10 && v.getA401() <= 10 && v.getA411() <= 10){
            v.setA42("基本是");
        }else{
            v.setA42("否");
        }
        if(ObjectUtil.isEmpty(list)){
            v.setNumber(1);
        }else{
            v.setNumber(list.get(0).getNumber() + 1);
        }
        Date now = new Date();
        User loginUser = getLoginUser();
        v.setCreateTime(now);
        v.setCreateUser(loginUser.getName());
        v.setUpdateTime(now);
        v.setUpdateUser(loginUser.getName());
        return service.save(v);
    }

    /**
     * 根据主键更新
     * @param v
     * @return
     */
    @ApiOperation(value = "根据主键更新数据",notes = "根据主键更新数据")
    @RequestMapping(method = RequestMethod.PUT)
    public boolean update(@RequestBody Annex5 v) throws Exception {
        Date now = new Date();
        v.setUpdateTime(now);
        v.setUpdateUser(getLoginUser().getName());
        // 判断是否是气虚质：2 3 4 14相加≥11分
        int count = v.getA2() + v.getA3() + v.getA4() + v.getA14();
        v.setA341(count);
        if(count >= 11){
            v.setA34("是");
        }else if(count >= 9){
            v.setA34("倾向是");
        }else{
            v.setA34("否");
        }
        // 判断是否是阳虚质：11 12 13 29相加≥11分
        count = v.getA11() + v.getA12() + v.getA13() + v.getA29();
        v.setA351(count);
        if(count >= 11){
            v.setA35("是");
        }else if(count >= 9){
            v.setA35("倾向是");
        }else{
            v.setA35("否");
        }
        // 判断是否是阴虚质：10 21 26 31相加≥11分
        count = v.getA10() + v.getA21() + v.getA26() + v.getA31();
        v.setA361(count);
        if(count >= 11){
            v.setA36("是");
        }else if(count >= 9){
            v.setA36("倾向是");
        }else{
            v.setA36("否");
        }
        // 判断是否是痰湿质：9 16 28 32相加≥11分
        count = v.getA9() + v.getA16() + v.getA28() + v.getA32();
        v.setA371(count);
        if(count >= 11){
            v.setA37("是");
        }else if(count >= 9){
            v.setA37("倾向是");
        }else{
            v.setA37("否");
        }
        // 判断是否是湿热质：23 25 27 30相加≥11分
        count = v.getA23() + v.getA25() + v.getA27() + v.getA30();
        v.setA381(count);
        if(count >= 11){
            v.setA38("是");
        }else if(count >= 9){
            v.setA38("倾向是");
        }else{
            v.setA38("否");
        }
        // 判断是否是血瘀质：19 22 24 33相加≥11分
        count = v.getA19() + v.getA22() + v.getA24() + v.getA33();
        v.setA391(count);
        if(count >= 11){
            v.setA39("是");
        }else if(count >= 9){
            v.setA39("倾向是");
        }else{
            v.setA39("否");
        }
        // 判断是否是气郁质：5 6 7 8相加≥11分
        count = v.getA5() + v.getA6() + v.getA7() + v.getA8();
        v.setA401(count);
        if(count >= 11){
            v.setA40("是");
        }else if(count >= 9){
            v.setA40("倾向是");
        }else{
            v.setA40("否");
        }
        // 判断是否是特禀质：15 17 18 20相加≥11分
        count = v.getA15() + v.getA17() + v.getA18() + v.getA20();
        v.setA411(count);
        if(count >= 11){
            v.setA41("是");
        }else if(count >= 9){
            v.setA41("倾向是");
        }else{
            v.setA41("否");
        }
        // 判断是否是平和质：1 2 4 5 13（2 4 5 13反向记分）得分≤17，同时其他体质得分≤8
        count = v.getA1() + (6 - v.getA2()) + (6 - v.getA4()) + (6 - v.getA5()) + (6 - v.getA13());
        v.setA421(count);
        if(count >= 17 && v.getA341() <= 8 && v.getA351() <= 8 && v.getA361() <= 8 && v.getA371() <= 8
            && v.getA381() <= 8 && v.getA391() <= 8 && v.getA401() <= 8 && v.getA411() <= 8){
            v.setA42("是");
        }else if(count >= 17 && v.getA341() <= 10 && v.getA351() <= 10 && v.getA361() <= 10 && v.getA371() <= 10
            && v.getA381() <= 10 && v.getA391() <= 10 && v.getA401() <= 10 && v.getA411() <= 10){
            v.setA42("基本是");
        }else{
            v.setA42("否");
        }
        QueryWrapper<Annex5> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", v.getId());
        return service.update(v,queryWrapper);
    }


    /**
     * 根据主键删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据主键删除数据",notes = "根据主键删除数据")
    @RequestMapping(method = RequestMethod.DELETE)
    public boolean removeById(@RequestParam Integer id){
        QueryWrapper<Annex5> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return service.remove(wrapper);
    }

}